package com.java.basis.collection;

import java.util.Comparator;
import java.util.TreeSet;

/**
 * @author wangning
 * @create 2021-04-08 16:14
 */
public class TestTreeSetRefeDataType02 {
	//2.比较器排序
	public static void main(String[] args) {
		//创建集合对象
		//TreeSet(Comparator<? super E> comparator) 构造一个新的空 TreeSet，它根据指定比较器进行排序。
		TreeSet<Student> ts=new TreeSet<Student>(new Comparator<Student>() {
			@Override
			public int compare(Student s1, Student s2) {
				// 姓名长度
				int num = s1.getName().length() - s2.getName().length();
				System.out.println("num = " + num);
				// 姓名内容
				int num2 = num == 0 ? s1.getName().compareTo(s2.getName()) : num;
				System.out.println(s1.getName().compareTo(s2.getName()));

				System.out.println("num2 = " + num2);
				// 年龄
				int num3 = num2 == 0 ? s1.getAge() - s2.getAge() : num2;
				System.out.println("num3 = " + num3);
				return num3;
			}
		});

		//创建元素对象
		Student s1=new Student("zhangsan",20);
		Student s2=new Student("lis",22);
		Student s3=new Student("wangwu",24);
		Student s4=new Student("chenliu",26);
		Student s5=new Student("zhangsan",22);
		Student s6=new Student("qianqi",24);

		//将元素对象添加到集合对象中
		ts.add(s1);
		ts.add(s2);
		ts.add(s3);
		ts.add(s4);
		ts.add(s5);
		ts.add(s6);

		//遍历
		for(Student s:ts){
			System.out.println(s.getName()+"-----------"+s.getAge());
		}
	}
}
